import HashHistory from './hash'
import HisHistory from './his'
import createMatcher from './matcher'
import Link from './router-link.js'
import View from './router-view.js'

export default class Router {
  constructor(options) {
    this.options = options
    this.mode = options.mode || 'hash'
    this.matcher = createMatcher(options.routes)
    if (this.mode == 'hash') {
      this.history = new HashHistory(this)
    } else if (this.mode == 'history') {
      this.history = new HisHistory(this)
    }
  }
  init(app) {
    this.app = app
    this.history.transitionTo()
  }
}

Router.install = function (Vue) {
  Vue.mixin({
    beforeCreate() {
      if (this.$options.router) {
        this._routerRoot = this
        this._router = this.$options.router
        this._router.init(this)
        Vue.util.defineReactive(this, '_route', this._router.matcher.current)
        Vue.component('router-link', Link)
        Vue.component('router-view', View)
      } else if (this.$parent) {
        this._routerRoot = this.$parent._routerRoot
        this._router = this.$parent._router
      }
    }
  })
}
